2014-09-10 4 views
0

Входной XML:XSL и добавить новый атрибут

<a> 
    <b1> 
     <c1 width="500" height="200"> 
      <d1 data="null" /> 
     </c1> 
    </b1> 
    <b2 /> 
</a> 

Я хочу копию все атрибуты из b1/c1 в b2/c1Идобавить новый атрибут (length). Вывод XML должен быть:

<a> 
    <b1> 
     <c1 width="500" height="200"> 
      <d1 data="null" /> 
     </c1> 
    </b1> 
    <b2> 
     <c1 width="500" height="200" length="2"> 
      <d1 data="null" /> 
     </c1> 
    </b2> 
</a> 

У меня есть код, который скопировать все от b1/с1 до b2/с2, НО без добавления нового atttribute (length):

<xsl:template match="https://stackoverflow.com/a/b2"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*" /> 
     <xsl:copy-of select="https://stackoverflow.com/a/b1/c1" /> 
     <xsl:apply-templates select="*" /> 
    </xsl:copy> 
</xsl:template> 

И я попробовал добавить атрибут к копии, но он не работает:

<xsl:template match="https://stackoverflow.com/a/b2"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*" /> 
     <xsl:copy-of select="https://stackoverflow.com/a/b1/c1" > 
      <xsl:attribute name="length">2</xsl:attribute> 
     </xsl:copy-of> 
     <xsl:apply-templates select="*" /> 
    </xsl:copy> 
</xsl:template> 

ответ

2

начните с шаблона идентификации, затем переопределите узел b2.

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:strip-space elements="*"/> 

    <xsl:output indent="yes"/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="b2"> 
     <xsl:copy> 
      <xsl:apply-templates select="../b1/c1" mode="test"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="c1" mode="test"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:attribute name="length">2</xsl:attribute> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

извините, я редактировал входной XML: нет c1 в b2, поэтому мне нужно-копия из. так как это сделать сейчас? – victorio

+0

отредактировали мой ответ. –

+0

спасибо, ты спас мою перевозчика! – victorio

2

Re отредактированный вопрос - попробуйте:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="b2"> 
    <xsl:copy> 
     <xsl:apply-templates select="../b1/c1" mode="copy"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="c1" mode="copy"> 
    <xsl:copy> 
     <xsl:attribute name="length">2</xsl:attribute> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
Смежные вопросы