使用 XSLT 减少 XML 输出

发布于 2024-10-31 05:01:24 字数 939 浏览 1 评论 0原文

如何使用 XSLT,从输入 xml 中仅选择一些 xml 标签到输出 XML? 输入示例:

<Country value="USA">
    <State value="KY>
        <City value="Hebron" />
        <City value="Lexington" />
        <City value="Owensboro" />
        <City value="Jonesville" />
    </State>
    <State value="OH">
         <City value="Cincinnati" />
         <City value="Columbus" />
         <City value="Cleveland" />
         <City value="Jonesville" />
    </State>
    <State value="IN" >
         <City value="Indianapolis" />
    </State>
 </Country>

那么,保留国家/州标签并仅复制希伯伦和辛辛那提?

预期输出:

<Country value="USA">
    <State value="KY>
        <City value="Hebron" />
    </State>
    <State value="OH">
         <City value="Cincinnati" />
    </State>
 </Country>

How can I using XSLT, select only some xml tags from my input xml to my output XML?
example input:

<Country value="USA">
    <State value="KY>
        <City value="Hebron" />
        <City value="Lexington" />
        <City value="Owensboro" />
        <City value="Jonesville" />
    </State>
    <State value="OH">
         <City value="Cincinnati" />
         <City value="Columbus" />
         <City value="Cleveland" />
         <City value="Jonesville" />
    </State>
    <State value="IN" >
         <City value="Indianapolis" />
    </State>
 </Country>

So, keep the Country/State tags in place and only copy Hebron and Cincinnati?

expected output:

<Country value="USA">
    <State value="KY>
        <City value="Hebron" />
    </State>
    <State value="OH">
         <City value="Cincinnati" />
    </State>
 </Country>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鲜血染红嫁衣 2024-11-07 05:01:24

以下样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="City[not(@value='Hebron' or @value='Cincinnati')]" />
</xsl:stylesheet>

在此输入上:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
        <City value="Lexington" />
        <City value="Owensboro" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
        <City value="Columbus" />
        <City value="Cleveland" />
    </State>
</Country>

产生以下结果:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
    </State>
</Country>

此样式表使用 身份转换 进行复制除了不需要的节点之外的所有节点的输出均保持不变。

另一个示例

您可能还想删除任何没有所需城市的 State 元素。此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="City[not(@value='Hebron' or @value='Cincinnati')]"/>
    <xsl:template 
           match="State[not(City[@value='Hebron' or @value='Cincinnati'])]"/>
</xsl:stylesheet>

应用于此输入:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
        <City value="Lexington" />
        <City value="Owensboro" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
        <City value="Columbus" />
        <City value="Cleveland" />
    </State>
    <State value="MO">
        <City value="St. Louis" />
    </State>
</Country>

生产:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
    </State>
</Country>

The following stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="City[not(@value='Hebron' or @value='Cincinnati')]" />
</xsl:stylesheet>

On this input:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
        <City value="Lexington" />
        <City value="Owensboro" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
        <City value="Columbus" />
        <City value="Cleveland" />
    </State>
</Country>

Produces the following result:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
    </State>
</Country>

This stylesheet uses the identity transform to copy all but the undesired nodes to the output unchanged.

Another example

You might also want to remove any State element that does not have a desired city. This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="City[not(@value='Hebron' or @value='Cincinnati')]"/>
    <xsl:template 
           match="State[not(City[@value='Hebron' or @value='Cincinnati'])]"/>
</xsl:stylesheet>

Applied to this input:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
        <City value="Lexington" />
        <City value="Owensboro" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
        <City value="Columbus" />
        <City value="Cleveland" />
    </State>
    <State value="MO">
        <City value="St. Louis" />
    </State>
</Country>

Produces:

<Country value="USA">
    <State value="KY">
        <City value="Hebron" />
    </State>
    <State value="OH">
        <City value="Cincinnati" />
    </State>
</Country>
流绪微梦 2024-11-07 05:01:24

这将只留下特定的城市:

<xsl:template match="*|@*">
    <xsl:copy><xsl:apply-templates select="*|@*"/></xsl:copy>
</xsl:template>


<xsl:template match="City[@value != 'Hebron' and @value != 'Cincinnati']"/>

这将只留下第一个城市:

<xsl:template match="*|@*">
    <xsl:copy><xsl:apply-templates select="*|@*"/></xsl:copy>
</xsl:template>


<xsl:template match="City[position() > 1]"/>

This will leave only specific cities:

<xsl:template match="*|@*">
    <xsl:copy><xsl:apply-templates select="*|@*"/></xsl:copy>
</xsl:template>


<xsl:template match="City[@value != 'Hebron' and @value != 'Cincinnati']"/>

This will leave only first city:

<xsl:template match="*|@*">
    <xsl:copy><xsl:apply-templates select="*|@*"/></xsl:copy>
</xsl:template>


<xsl:template match="City[position() > 1]"/>
情痴 2024-11-07 05:01:24

这是我的(可能不充分)2.0 解决方案。城市是作为参数传递的正则表达式。

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:param name="Cities" select="'Cincinnati|Hebron'"/>

  <xsl:template match="State">
    <xsl:if test="exists(City[matches(@value, $Cities)])">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

  <xsl:template match="State/City">
    <xsl:if test="matches(@value, $Cities)">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
   </xsl:template>

   <xsl:template match="@*|node()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
   </xsl:template>

</xsl:stylesheet>

Here's my (probably inadequate) 2.0 solution. Cities are a regular expression passed as a parameter.

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:param name="Cities" select="'Cincinnati|Hebron'"/>

  <xsl:template match="State">
    <xsl:if test="exists(City[matches(@value, $Cities)])">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

  <xsl:template match="State/City">
    <xsl:if test="matches(@value, $Cities)">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
   </xsl:template>

   <xsl:template match="@*|node()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
   </xsl:template>

</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文