2015-01-23 3 views
0

У меня возникла проблема с перемещением определенных элементов из коллекции в новый родительский узел. Я знаю, как выбирать эти элементы (здесь: root/order/person [position()> 1] для msxml), но не может найти правильное использование xslt: copy или xslt: copy-of statements.XSLT: переместить определенные элементы в новый родительский

Это то, что я есть (например):

<root> 
    <order> 
    <person> 
     ... 
    </person> 
    <person> 
     ... 
    </person> 
    <person> 
     ... 
    </person> 
    </order> 
</root> 

И я просто хочу, чтобы поставить человека элементы (1-п) каждый в единый порядок элемента:

<root> 
    <order> 
    <person> 
     ... 
    </person> 
    </order> 
    <order> 
    <person> 
     ... 
    </person> 
    </order> 
    <order> 
    <person> 
     ... 
    </person> 
    </order> 
</root> 

I уже достигнутый, чтобы удалить элементы человека, кроме первого, из элемента заказа. Но теперь мне трудно перемещать оставшиеся элементы человека во вновь созданные элементы заказа.

ответ

0

Я знаю, как выбрать эти элементы (здесь: корня/заказ/человек [позиция()> 1] для MSXML)

...

И я просто хочу, чтобы поставить элементы человек (1-п) каждый в единый элемент порядка:

либо я что-то пропустил или вы делаете это гораздо сложнее, чем это должно быть. Попробуйте:

XSLT 1,0

<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:template match="/root"> 
    <root> 
     <xsl:for-each select="order/person"> 
      <order> 
       <xsl:copy-of select="."/> 
      </order> 
     </xsl:for-each> 
    </root> 
</xsl:template> 

</xsl:stylesheet> 

или:

XSLT 1,0

<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> 

<!-- skip order wrapper --> 
<xsl:template match="order"> 
    <xsl:apply-templates select="@*|node()"/> 
</xsl:template> 

<!-- add order wrapper to each person --> 
<xsl:template match="person"> 
    <order> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </order> 
</xsl:template> 

</xsl:stylesheet> 
0

Как я хотел бы сделать это, чтобы переопределить шаблон персонами (прочитать на как это сделать). Это делает копирование дерева ввода по умолчанию, за исключением того, что вы переопределите бит.

<!-- import the identity template --> 
<xsl:import href="identity.xsl"/> 

<!-- this removes the original order element that wrapped the persons --> 
<xsl:template match="order"> 
    <xsl:apply-templates/> 
</xsl:template> 

<!-- this creates an order element around each person. --> 
<xsl:template match="person"> 
    <order> 
    <!-- Because of the identity template override the old person element gets copied here --> 
    <xsl:apply-imports/> 
    </order> 
</xsl:template> 
Смежные вопросы