只是一个简单的问题。我有一个XML,我希望只改变它的一部分而不改变其他任何东西。以下是我要做的一个简单示例:
输入:
<?xml version="1.0" encoding="UTF-8"?>
<dita xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../xsd/ditabase.xsd">
<topic id="abc">
<title>Sample XML</title>
<body>
<section id="a">
<p> Hello section A </p>
</section>
<section id="b">
<p> General Content </p>
</section>
<section id="c">
<p> Hi thank you for coming from $state </p>
</section>
</body>
</topic>
</dita>
输出
<?xml version="1.0" encoding="UTF-8"?>
<dita xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../xsd/ditabase.xsd">
<topic id="abc">
<title>Sample XML</title>
<body>
<section id="a">
<p> Hello section A </p>
</section>
<section id="b">
<p> General Content </p>
</section>
<section id="c" audience = "WA">
<p> Hi thank you for coming from WA </p>
</section>
<section id="c" audience = NY">
<p> Hi thank you for coming from NY </p>
</section>
<section id="c" audience = "AL">
<p> Hi thank you for coming from AL </p>
</section>
<section id="c" audience = "GA">
<p> Hi thank you for coming from GA </p>
</section>
...
<!--Continue for the rest of the states-->
...
</body>
</topic>
</dita>
我正在使用XALAN处理器,如果这可以帮助。非常感谢提前:D
答案 0 :(得分:2)
我建议您稍微更改一下XML 的格式,以使叙述更简单:
而不是:
<p> Hi thank you for coming from $state </p>
使用强>:
<p> Hi thank you for coming from <state/> </p>
对于此格式,进行以下转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<my:states>
<state name="WA"/>
<state name="NY"/>
<state name="AL"/>
<state name="GA"/>
</my:states>
<xsl:variable name="vStates" select="document('')/*/my:states/*"/>
<xsl:template match="node()|@*">
<xsl:param name="pState"/>
<xsl:copy>
<xsl:apply-templates select="node()|@*">
<xsl:with-param name="pState" select="$pState"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="section[descendant::state]">
<xsl:variable name="vSect" select="."/>
<xsl:for-each select="$vStates">
<xsl:apply-templates select="$vSect" mode="generate">
<xsl:with-param name="pState" select="@name"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="section" mode="generate">
<xsl:param name="pState"/>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="audience">
<xsl:value-of select="$pState"/>
</xsl:attribute>
<xsl:apply-templates>
<xsl:with-param name="pState" select="$pState"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="state">
<xsl:param name="pState"/>
<xsl:value-of select="$pState"/>
</xsl:template>
</xsl:stylesheet>
应用于(略微修改过的)提供的XML文档:
<dita xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../xsd/ditabase.xsd">
<topic id="abc">
<title>Sample XML</title>
<body>
<section id="a">
<p> Hello section A </p>
</section>
<section id="b">
<p> General Content </p>
</section>
<section id="c">
<p> Hi thank you for coming from <state/> </p>
</section>
</body>
</topic>
</dita>
生成想要的正确结果:
<dita xsi:noNamespaceSchemaLocation="../../../xsd/ditabase.xsd" xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<topic id="abc">
<title>Sample XML</title>
<body>
<section id="a">
<p> Hello section A </p>
</section>
<section id="b">
<p> General Content </p>
</section>
<section id="c" audience="WA">
<p> Hi thank you for coming from WA </p>
</section>
<section id="c" audience="NY">
<p> Hi thank you for coming from NY </p>
</section>
<section id="c" audience="AL">
<p> Hi thank you for coming from AL </p>
</section>
<section id="c" audience="GA">
<p> Hi thank you for coming from GA </p>
</section>
</body>
</topic>
</dita>