首页 > 解决方案 > 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。

<?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>

感谢您提出的任何建议,因为我不是做这类事情的全职工作。:)

标签: xmlxslt

解决方案


您可以使用以下 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>

它是Identity 模板和空模板的组合,可确保不复制匹配的元素。


另一种方法是白名单方法。它屏蔽所有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>

推荐阅读