0
我試圖改變這樣的數據:如何僅基於特定節點的屬性值修改其他節點?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<branch link="1" type="band" value="">
<leaf/>
<branch link="1" type="band/mhz" value="some">
<leaf/>
</branch>
<branch link="1" type="band/other" value="thing">
<leaf/>
</branch>
</branch>
<branch link="1" type="member" value="">
<leaf/>
<branch link="1" type="member/kind" value="a">
<leaf/>
</branch>
</branch>
<branch link="2" type="member" value="">
<leaf/>
<branch link="2" type="member/kind" value="b">
<leaf/>
</branch>
</branch>
</root>
得到這個:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<branch link="" type="other" value="thing">
<leaf/>
</branch>
<branch link="" type="member-info" value="">
<leaf/>
<branch link="" type="member-info/kind" value="a">
<leaf/>
</branch>
</branch>
<branch link="2" type="member" value="">
<leaf/>
<branch link="2" type="member/kind" value="b">
<leaf/>
</branch>
</branch>
</root>
,目前我使用這個XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" method="xml"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="branch[starts-with(@name, 'band')]">
<xsl:for-each select="current()/*">
<xsl:if test="@name = 'band/other'">
<xsl:variable name="linkId" select="@link"/>
<xsl:for-each select="../../branch[@link=$linkId and @name='member']">
<branch>
<xsl:attribute name="name">
<xsl:value-of select="replace(@name, 'member', 'member-info')"/>
</xsl:attribute>
<xsl:for-each select="@*[.]">
<xsl:if test="local-name() != 'name'">
<xsl:attribute name="{local-name()}">
<xsl:if test="local-name() != 'link'">
<xsl:value-of select="."/>
</xsl:if>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="./*">
<branch>
<xsl:attribute name="name">
<xsl:value-of
select="replace(@name, 'member', 'member-info')"/>
</xsl:attribute>
<xsl:for-each select="@*[.]">
<xsl:if test="local-name() != 'name'">
<xsl:attribute name="{local-name()}">
<xsl:if test="local-name() != 'link'">
<xsl:value-of select="."/>
</xsl:if>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates/>
</branch>
</xsl:for-each>
</branch>
<xsl:apply-templates/>
</xsl:for-each>
<xsl:copy>
<xsl:attribute name="name">
<xsl:value-of select="replace(@name, 'band/', '')"/>
</xsl:attribute>
<xsl:for-each select="@*[.]">
<xsl:if test="local-name() != 'name'">
<xsl:attribute name="{local-name()}">
<xsl:if test="local-name() != 'link'">
<xsl:value-of select="."/>
</xsl:if>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="branch[@name='member' and @link='']"/>
</xsl:stylesheet>
所以,我試圖:
「動起來邏輯電平」的元素匹配
<branch type="band/other">
得到<branch type="other">
在清除link
屬性和丟棄的父母和兄弟姐妹 - >OK然後,從第1部分元素,找到
<branch type="member">
其中有相同的link
屬性。值(例如「1」)和通過將「-info」屬性的爲「構件」值重命名它們(以及它們的branch
子女)type
- >NOK保持其它元素,因爲它們是 - >行
問題與第2部分:
- 原始(unrenamed,鏈接= 「1」) 「部件/一種」 保持
- 原(unrenamed,鏈接= 「1」) 「成員」 和兒童 「成員/一種」 保持
我怎樣才能解決這些?我是否需要一種不同的方法(使用名爲tempalted)?
不幸的是,我沒有訪問XSLT 3.0(雖然我一直想變化一段時間的變化)。儘管如此,2.0版本完美運作。謝謝! – Saran