XSLT:确保存在具有默认内容的某些元素
|
我需要确保XML文档始终包含以下节点:
<group>
<section>0001</section>
<head>0002</head>
<body>0003</body>
</group>
典型的XML输入文件如下所示(组节点应始终位于类别和摘要之前):
<story>
<group>
<section>section-content</section>
<head>head-content</head>
<body>body-content</body>
<extra>extra-content</extra>
</group>
<category>some text</category>
<summary>some text</summary>
</story>
但是不能保证group元素或其任何子元素将存在。
因此,给出一个XML文档:
<story>
<category>some text</category>
<summary>some text</summary>
</story>
输出应如下所示:
<story>
<group>
<section>0001</section>
<head>0002</head>
<body>0003</body>
</group>
<category>some text</category>
<summary>some text</summary>
</story>
XSLT不应修改现有内容,例如。
<story>
<group>
<section>existing text</section>
<extra>existing text</extra>
</group>
<category>some text</category>
<summary>some text</summary>
</story>
应转换为:
<story>
<group>
<section>existing text</section>
<head>0002</head>
<body>0003</body>
<extra>existing text</extra>
</group>
<category>some text</category>
<summary>some text</summary>
</story>
以Tomalak的答案为基础,我想到了这一点:
<xsl:stylesheet version=\"1.0\"
xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"
xmlns:subst=\"http://tempuri.com/mysubst\"
>
<xsl:strip-space elements=\"*\"/>
<xsl:output method=\"xml\" encoding=\"utf-8\" indent=\"yes\"/>
<!-- These defaults (elements and contents) can be modified at any time -->
<subst:defaults>
<subst:element name=\"group\">
<subst:section>0001</subst:section>
<subst:head>0002</subst:head>
<subst:body>0002</subst:body>
</subst:element>
</subst:defaults>
<!-- this makes the above available as a variable -->
<xsl:variable name=\"defaults\" select=\"document(\'\')/*/subst:defaults\" />
<xsl:template match=\"@*|node()\">
<xsl:copy>
<xsl:apply-templates select=\"@*|node()\"/>
</xsl:copy>
</xsl:template>
<xsl:template match=\"story[not(group)]\">
<xsl:copy>
<xsl:apply-templates select=\"@*\"/>
<xsl:element name=\"group\"></xsl:element>
<xsl:apply-templates select=\"node()\"/>
</xsl:copy>
</xsl:template>
<xsl:template match=\"group\">
<xsl:copy>
<xsl:copy-of select=\"@*|*\"/>
<xsl:call-template name=\"create-defaults\" />
</xsl:copy>
</xsl:template>
<!-- Insert the defaults-->
<xsl:template name=\"create-defaults\">
<xsl:variable name=\"this\" select=\".\" />
<xsl:for-each select=\"$defaults/subst:element[@name = name($this)]/*\">
<xsl:if test=\"not($this/*[name() = local-name(current())])\">
<xsl:apply-templates select=\".\" />
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Remove the namespaces -->
<xsl:template match=\"*\">
<xsl:element name=\"{local-name()}\">
<xsl:apply-templates select=\"@*|node()\"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
如果group元素已经存在,这似乎可行,但是如果group元素不存在,我想不通如何使它工作。
没有找到相关结果
已邀请:
1 个回复
厘恼轨
应用于此输入时
产生: