【发布时间】:2016-10-28 10:58:12
【问题描述】:
我现在多次更改我的 xsl,但我没有找到正确的方法。现在这就是我想要的:
我现在尝试查找所有丢失的页面,但没有描述。如果缺少,我想为该页面添加描述,如果存在,我想修改描述。 pageX 到 pageXDescription 的字符串始终相同。
这是我的简短示例 xml:
<BOOK>
<PAGE NAME='page1' VALUE='coolText'/>
<PAGE NAME='Description1' VALUE='coolDescription'/>
<PAGE NAME='page2' VALUE='moreText'/>
<PAGE NAME='page3' VALUE='aLotMoreText'/>
<PAGE NAME='Description3' VALUE='aLotMoreDescriptions'/>
</BOOK>
我尝试过类似的东西:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- define output settings and header -->
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" media-type="string" encoding="ISO-8859-1" doctype-system="deftable.dtd"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK[PAGE/@NAME='page1']">
<xsl:copy>
<xsl:call-template name="create_missing_description_pages">
<xsl:with-param name="page" select="'page1'"/>
<xsl:with-param name="description" select="'Description1'"/>
<xsl:with-param name="new_description" select="'newContent'"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK[PAGE/@NAME='page2']">
<xsl:copy>
<xsl:call-template name="create_missing_description_pages">
<xsl:with-param name="page" select="'page2'"/>
<xsl:with-param name="description" select="'Description2'"/>
<xsl:with-param name="new_description" select="'newContent'"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK[PAGE/@NAME='page3']">
<xsl:copy>
<xsl:call-template name="create_missing_description_pages">
<xsl:with-param name="page" select="'page3'"/>
<xsl:with-param name="description" select="'Description3'"/>
<xsl:with-param name="new_description" select="'newContent'"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<!-- Function to generate missing XML Tags -->
<xsl:template name="create_missing_description_pages">
<xsl:param name="page"/>
<xsl:param name="description"/>
<xsl:param name="new_description"/>
<xsl:apply-templates select="@*|VARIABLE[@NAME=$page]/preceding-sibling::node()"/>
<xsl:apply-templates select="VARIABLE[@NAME=$page]"/>
<xsl:if test="not(VARIABLE/@NAME=$description)">
<xsl:element name="PAGE">
<xsl:attribute name="NAME"><xsl:value-of select="$description"/></xsl:attribute>
<xsl:attribute name="VALUE"><xsl:value-of select="$new_description"/></xsl:attribute>
</xsl:element>
</xsl:if>
<xsl:apply-templates select="VARIABLE[@NAME=$page]/following-sibling::node()"/>
</xsl:template>
<xsl:template match="BOOK/PAGE">
<xsl:copy>
<xsl:choose>
<xsl:when test="@NAME='Description1'">
<xsl:attribute name="NAME"><xsl:value-of select="@NAME"/></xsl:attribute>
<xsl:attribute name="VALUE">newContent</xsl:attribute>
</xsl:when>
<xsl:when test="@NAME='Description2'">
<xsl:attribute name="NAME"><xsl:value-of select="@NAME"/></xsl:attribute>
<xsl:attribute name="VALUE">newContent</xsl:attribute>
</xsl:when>
<xsl:when test="@NAME='page3Description'">
<xsl:attribute name="NAME"><xsl:value-of select="@NAME"/></xsl:attribute>
<xsl:attribute name="VALUE">newContent</xsl:attribute>
</xsl:when>
<!-- other child items will just be copied -->
<xsl:otherwise>
<xsl:copy-of select="@*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这是我在所有情况下所期望的,如果缺少所有描述,或者如果所有描述都可用:
<BOOK>
<PAGE NAME='page1' VALUE='coolText'/>
<PAGE NAME='Description1' VALUE='newContent'/>
<PAGE NAME='page2' VALUE='moreText'/>
<PAGE NAME='Description2' VALUE='newContent'/>
<PAGE NAME='page3' VALUE='aLotMoreText'/>
<PAGE NAME='Description3' VALUE='newContent'/>
</BOOK>
如果没有page3,那么我也想要没有page Description。
我希望如此,这是可以理解的。
非常感谢您的提示,我的逻辑错误在哪里以及如何解决它。
最好的问候 比约恩
【问题讨论】:
-
在您的 XML 输入中,
page1和page1Description等是真实姓名吗,即是否有一个方案可以匹配 XSLT 的命名? -
嗨,马丁,感谢您的回复。是的,他们到底是真名,但我生成这个例子只是为了说明我的问题。真正的 xml 要复杂得多。这不是一个好的 XML 结构,但我无法更改输入结构。希望能回答你的问题。
标签: xslt xml-parsing xslt-2.0