XSLT文件的XML输入限制结果



首先让我开始吧,我只是在尝试XML,没有受过正式的培训,所以这可能是一个非常基本的问题,但对我来说是一个疏忽。

我有一个与此类似的XML文件。

<?xml version='1.0' encoding='UTF-8' ?>
<document>
<properties>
<basic>
<property id="number">
<value>305</value>
</property>
<property id="first">
<value>given</value>
</property>
<property id="last">
<value>family</value>
</property>
</basic>
</properties>
</document>

然后,我需要用XML显示类似于此的输出。

<document>
<properties>
<basic>
<property id="number">
<value>305</value>
</property>
<property id="last">
<value>family</value>
</property>
</basic>
</properties>
</document>

我只是将某些属性id值包括到最终目的地。

我已经能够用CSV来做这件事,但不能用XML作为输出,也不确定应该从哪里开始。

示例XSL,它给了我一个,deliminator值。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="preg" match="property" use="@id"/>
<xsl:output method="text"></xsl:output>
<xsl:strip-space elements="*" />
<xsl:template match="/">

<!-- Item 1 Value -->
<xsl:for-each select="key('preg','number')">
<xsl:value-of select="value"/>
<xsl:text disable-output-escaping="yes">,</xsl:text>
</xsl:for-each>
<!-- Item 2 Value -->
<xsl:for-each select="key('preg','last')">
<xsl:value-of select="value"/>
<xsl:text disable-output-escaping="yes">,</xsl:text>
</xsl:for-each>
<xsl:text>&#13;</xsl:text>
<xsl:text>&#10;</xsl:text>
</xsl:template>
</xsl:stylesheet>

谢谢你的任何建议,因为我这不是我做这些事情的全职工作。:(

您可以使用以下XSLT-1.0样式表生成所需的结果,这是一种黑名单方法。这个解决方案只是省略了某些(列入黑名单的(元素,并复制了其余元素

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<!-- Identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template> 
<xsl:template match="property[@id='first']" />
</xsl:stylesheet>

它是标识模板和空模板的组合,确保匹配的元素不会被复制


另一种方法是白名单方法。它屏蔽所有property元素,然后只复制复制模板中定义的元素:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<!-- Identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template> 
<!-- Omit all 'property' elements... -->
<xsl:template match="property" />
<!-- ...except for those with an @id of 'number' or 'last' -->
<xsl:template match="property[@id='number' or @id='last']">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template> 
</xsl:stylesheet>

两种情况的结果都是:

<?xml version="1.0"?>
<document>
<properties>
<basic>
<property id="number">
<value>305</value>
</property>
<property id="last">
<value>family</value>
</property>
</basic>
</properties>
</document>

最新更新