1

下面是我的 XML 的结构。

<Client>
  <Document>
    <service class="ABC">
      <author name="p"/>
      <title>t1</title>
      <Details>
        ...
      </Details>
    </service>
   </Document>
   <Document>
     <service class="XYZ">
       <author name="a"/>
       <title>t2</title>
       <Details>
         ...
       </Details>
     </service>
   </Document>
   <Document>
     <service class="PQR">
       <author name="x"/>
       <title>t3</title>
       <Details>
         ...
       </Details>
     </service>
   </Document>
</Client>

我想创建标题为“t2”但无法找出解决方案的文档节点的副本。输出应该看起来像

<Client>
  <Document>
    <service class="ABC">
      <author name="p"/>
      <title>t1</title>
      <Details>
        ...
      </Details>
    </service>
   </Document>
   <Document>
     <service class="XYZ">
       <author name="a"/>
       <title>t2</title>
       <Details>
         ...
       </Details>
     </service>
   </Document>
   <Document>
     <service class="PQR">
       <author name="x"/>
       <title>t3</title>
       <Details>
         ...
       </Details>
     </service>
   </Document>
   <Document>
     <service class="XYZ">
       <author name="a"/>
       <title>t2</title>
       <Details>
         ...
       </Details>
     </service>
   </Document>
</Client>

感谢您的帮助,非常感谢。

4

2 回答 2

1

这个简单的样式表是这样做的一种方法:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

  <!--
  Identity transform
  See http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT
  -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Override identity transform to process <Client> element -->
  <xsl:template match="Client">
    <!-- Copy current element into output -->
    <xsl:copy>
      <!-- Apply attributes and child nodes -->
      <xsl:apply-templates select="@* | node()"/>
      <!-- Apply all <Document> elements with <title> whose value is "t2" -->
      <xsl:apply-templates select="Document[service/title = 't2']"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
于 2013-04-22T19:04:20.813 回答
0

先前的答案本来是正确的,但对于该行:

经验证的正确版本为:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

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

<!-- Override identity transform to process <Client> element -->
<xsl:template match="Client">
    <!-- Copy current element into output -->
    <xsl:copy>
        <!-- Apply attributes and child nodes -->
        <!-- Apply all <Document> elements with <title> whose value is "t2" -->
        <xsl:apply-templates select="Document[service/title = 't2']"/>
    </xsl:copy>
</xsl:template>

输出是:

<?xml version="1.0" encoding="utf-8"?>
<Client>
   <Document>
        <service class="XYZ">
            <author name="a"/>
            <title>t2</title>
            <Details>
               ...
            </Details>
        </service>
    </Document>
</Client>
于 2015-11-24T15:28:34.503 回答