选择节点而不丢失属性

huangapple go评论67阅读模式
英文:

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.

&lt;section&gt;
  &lt;line number=&#39;1&#39; style=&#39;none&#39; bold=&#39;true&#39; size=&#39;10pt&#39;&gt;Line 1&lt;/line&gt;
  &lt;line number=&#39;2&#39; bold=&#39;true&#39; &gt;Line 2&lt;/line&gt;
  &lt;line number=&#39;3&#39; style=&#39;none&#39; bold=&#39;true&#39; size=&#39;10pt&#39; color=&#39;red&#39;&gt;Line 3&lt;/line&gt;
&lt;/section&gt;

This i want to transform to

&lt;div&gt;
      &lt;p num=&#39;1&#39; style=&#39;none&#39; bold=&#39;true&#39; size=&#39;10pt&#39;&gt;Line 1&lt;/p&gt;
      &lt;p num=&#39;2&#39; b=&#39;true&#39; &gt;Line 2&lt;/p&gt;
      &lt;p num=&#39;3&#39; style=&#39;none&#39; bold=&#39;true&#39; size=&#39;10pt&#39; color=&#39;red&#39;&gt;Line 3&lt;/p&gt;
&lt;/div&gt;

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.

&lt;xsl:template match=&quot;section&quot;&gt;&lt;div use-attribute-sets=&quot;default&quot;&gt;&lt;xsl:apply-templates select=&quot;node()&quot;/&gt;&lt;/div&gt;&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;p&quot;&gt;&lt;p use-attribute-sets=&quot;default&quot;&gt;&lt;xsl:apply-templates select=&quot;node()&quot;/&gt;&lt;/p&gt;&lt;/xsl:template&gt;

		&lt;xsl:attribute-set name=&quot;default&quot;&gt;
			&lt;xsl:attribute name=&quot;number&quot;&gt;&lt;xsl:value-of select=&quot;@num&quot;/&gt;&lt;/xsl:attribute&gt;
			&lt;xsl:attribute name=&quot;style&quot;&gt;&lt;xsl:value-of select=&quot;@style&quot;/&gt;&lt;/xsl:attribute&gt;
            &lt;xsl:attribute name=&quot;b&quot;&gt;&lt;xsl:value-of select=&quot;@bold&quot;/&gt;&lt;/xsl:attribute&gt;
            &lt;xsl:attribute name=&quot;size&quot;&gt;&lt;xsl:value-of select=&quot;@size&quot;/&gt;&lt;/xsl:attribute&gt;
            &lt;xsl:attribute name=&quot;color&quot;&gt;&lt;xsl:value-of select=&quot;@color&quot;/&gt;&lt;/xsl:attribute&gt;
		&lt;/xsl:attribute-set&gt;

答案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:

&lt;xsl:template match=&quot;*&quot;&gt; &lt;!-- This matches all nodes. Note that specific templates have higher priority and will hit earlier (You can probably use your match=&quot;p&quot; or &quot;line&quot;) --&gt;
    &lt;xsl:apply-templates select=&quot;@*&quot;/&gt; &lt;!-- Default value if no select is given is &#39;*&#39; so no attributes would be hit --&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;@*&quot;&gt; &lt;!-- Match all the attributes so you can copy them --&gt;
    &lt;xsl:copy/&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;@bold&quot;&gt; &lt;!-- Note that this will hit instead of @* because its more specific as described above --&gt;
    &lt;xsl:attribute name=&quot;b&quot; select=&quot;.&quot;/&gt;
&lt;/xsl:template&gt;
&lt;!-- Here you can specify more of such attribute matches if needed --&gt;

huangapple
  • 本文由 发表于 2020年8月13日 15:54:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63390564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定