英文:
XSLT select node without loosing attributes
问题
以下是翻译的内容:
我正在尝试对XML标记进行XSLT转换,输出结果与期望的一致,但所有属性值都被修剪掉了。我希望所有属性都能被应用,但特定的属性需要被修改,比如属性名称。
请看下面的示例。
<section>
<line number='1' style='none' bold='true' size='10pt'>Line 1</line>
<line number='2' bold='true' >Line 2</line>
<line number='3' style='none' bold='true' size='10pt' color='red'>Line 3</line>
</section>
我想要将其转换为
<div>
<p num='1' style='none' bold='true' size='10pt'>Line 1</p>
<p num='2' b='true' >Line 2</p>
<p num='3' style='none' bold='true' size='10pt' color='red'>Line 3</p>
</div>
这是我迄今为止编写的示例。但它比较复杂,因为我不能假设哪些属性会被应用,所以我不想显式提供属性名称。
<xsl:template match="section"><div use-attribute-sets="default"><xsl:apply-templates select="node()"/></div></xsl:template>
<xsl:template match="p"><p use-attribute-sets="default"><xsl:apply-templates select="node()"/></p></xsl:template>
<xsl:attribute-set name="default">
<xsl:attribute name="number"><xsl:value-of select="@num"/></xsl:attribute>
<xsl:attribute name="style"><xsl:value-of select="@style"/></xsl:attribute>
<xsl:attribute name="b"><xsl:value-of select="@bold"/></xsl:attribute>
<xsl:attribute name="size"><xsl:value-of select="@size"/></xsl:attribute>
<xsl:attribute name="color"><xsl:value-of select="@color"/></xsl:attribute>
</xsl:attribute-set>
英文:
I am trying to do xslt transformation on xml tags and the output is coming as expected but all attribute value are getting trimmed off . I want to have all attributes need to be applied but specific attributes need to be modified like attribute names.
Please find below example.
<section>
<line number='1' style='none' bold='true' size='10pt'>Line 1</line>
<line number='2' bold='true' >Line 2</line>
<line number='3' style='none' bold='true' size='10pt' color='red'>Line 3</line>
</section>
This i want to transform to
<div>
<p num='1' style='none' bold='true' size='10pt'>Line 1</p>
<p num='2' b='true' >Line 2</p>
<p num='3' style='none' bold='true' size='10pt' color='red'>Line 3</p>
</div>
This is the example i have write so far. But is but complex because i cant assume which attribute applied so i don't want to give the names of attributes explicitly.
<xsl:template match="section"><div use-attribute-sets="default"><xsl:apply-templates select="node()"/></div></xsl:template>
<xsl:template match="p"><p use-attribute-sets="default"><xsl:apply-templates select="node()"/></p></xsl:template>
<xsl:attribute-set name="default">
<xsl:attribute name="number"><xsl:value-of select="@num"/></xsl:attribute>
<xsl:attribute name="style"><xsl:value-of select="@style"/></xsl:attribute>
<xsl:attribute name="b"><xsl:value-of select="@bold"/></xsl:attribute>
<xsl:attribute name="size"><xsl:value-of select="@size"/></xsl:attribute>
<xsl:attribute name="color"><xsl:value-of select="@color"/></xsl:attribute>
</xsl:attribute-set>
答案1
得分: 1
If you want to copy all existing attributes by default with some exceptions you can achieve that like the following code:
<xsl:template match="*"> <!-- This matches all nodes. Note that specific templates have higher priority and will hit earlier (You can probably use your match="p" or "line") -->
<xsl:apply-templates select="@*"/> <!-- Default value if no select is given is ' * ' so no attributes would be hit -->
</xsl:template>
<xsl:template match="@*"> <!-- Match all the attributes so you can copy them -->
<xsl:copy/>
</xsl:template>
<xsl:template match="@bold"> <!-- Note that this will hit instead of @* because its more specific as described above -->
<xsl:attribute name="b" select="."/>
</xsl:template>
<!-- Here you can specify more of such attribute matches if needed -->
英文:
If you want to copy all existing attributes by default with some exceptions you can achieve that like the following code:
<xsl:template match="*"> <!-- This matches all nodes. Note that specific templates have higher priority and will hit earlier (You can probably use your match="p" or "line") -->
<xsl:apply-templates select="@*"/> <!-- Default value if no select is given is '*' so no attributes would be hit -->
</xsl:template>
<xsl:template match="@*"> <!-- Match all the attributes so you can copy them -->
<xsl:copy/>
</xsl:template>
<xsl:template match="@bold"> <!-- Note that this will hit instead of @* because its more specific as described above -->
<xsl:attribute name="b" select="."/>
</xsl:template>
<!-- Here you can specify more of such attribute matches if needed -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论