Quickstart
Quickstart
インストールからスタイル付きドキュメントまでわずか5分:このページは小さなDOMを構築し、スタイルシートを適用し、解決されたスタイルを読み戻します。
Prerequisites
| Requirement | Detail |
|---|---|
| Python | 3.10 以降 |
| Package | aspose-html-foss インストール済み (Installation) |
1. ドキュメントと要素を作成する
Document はすべてのノードタイプのファクトリです。要素を作成し、属性を付与し、ツリーに添付します:
from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)2. スタイルシートを添付する
CSSStyleSheet.replace_sync() で CSS テキストを解析し、シートをドキュメントに登録してカスケードに参加させます:
from aspose_html.cssom import CSSStyleSheet
sheet = CSSStyleSheet()
sheet.replace_sync(".foo { color: red } #bar { color: blue }")
doc.attach_style_sheet(sheet)3. 計算されたスタイルを読む
Element.get_computed_style() は特異性、重要度、継承を解決します。ここでは ID セレクタがクラスセレクタよりも優先されます:
style = el.get_computed_style()
print(style.get_property_value("color"))4. インラインスタイルを試す
要素の style 宣言を介して設定されたインライン宣言は、作者のスタイルシート規則よりも優先されます:
inline = el.style
inline.set_property("color", "green")
print(el.get_computed_style().get_property_value("color"))