XSL问题:制作选择性xml但条件为

时间:2016-07-10 10:10:30

标签: xml xslt

我有以下输入xml:

<class xmlns:xfa="soommmeee-tteexxxxtttttt">
    <students>
        ----------
        -----------
        ----------
    </students>
    <students>
        <id>B</id>
        <name>jzvxcbhjnba</name>
        <description>mjZSVxcj</description>
        <student>
            <id>3</id>
            <refObjectId>m8</refObjectId>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <friend>hercules</friend>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>aksbgdk</fragId>
                            <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
                            <zmdcb>slzdhfksjfml</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>3</id>
            <refObjectId>m8</refObjectId>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <friend>aladin</friend>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>aksbgdk</fragId>
                            <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
                            <zmdcb>slzdhfksjfml</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>3</id>
            <refObjectId>m8</refObjectId>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <friend>aladin</friend>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>aksbgdk</fragId>
                            <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
                            <zmdcb>slzdhfksjfml</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>4</id>
            <refObjectId>m3</refObjectId>
            <subject>
                <id>75</id>
                <name>PCM</name>
                <friend>hercules</friend>
                <firstname>
                    <textContent>
                        <Id>j4423</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>dfzxsdbjchA</body>
                        </text>
                        <Key>zdbgcfjkbna</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>zdjbhfjkcda</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>sdjkhzjk</body>
                        </text>
                        <Key>mzsdjkbjk</Key>
                    </textContent>
                    <isDisplay>true</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>sdbnfn</fragId>
                            <znjdvgsc>masdjkljk</znjdvgsc>
                            <zmdcb>msjkldbhfjkn</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
    </students>
    <students>
    ------------
    --------------
    </students>
    <faculty>
        <configuration>
            <Type>Output</Type>
            <Key>Font</Key>
            <Value>10pt</Value>
        </configuration>
        <configuration>
            <Type>Out</Type>
            <Key>Text</Key>
            <Value>127,127,127</Value>
        </configuration>
        <configuration>
            <Type>put</Type>
            <Key>Slide</Key>
            <Value>18pt</Value>
        </configuration>
        <configuration>
            <Type>utp</Type>
            <Key>Slide</Key>
            <Value>127</Value>
        </configuration>
    </faculty>
    <Info />
    <Dean>
        <name>zcdfjkaqbkd</name>
    </Dean>
</class>    

我想要一个xsl来取出整个“学生”的部分。包括&#39;学生&#39;的小节。但只有那些学生应该被包含在新的xml中,而这个新的xml中有他们的朋友作为&#39; hercules&#39;。这并不是一个小小的&#39;赫拉克勒斯&#39;将由任何一个学生组成,也可以在不止一个学生中,也可能在不同的学生之下。部分也是

我对此没有任何想法....我已经经历了很多例子,但无法做到这一点

说我想要的输出是:

<?xml version="1.0" encoding="UTF-8"?>
<class xmlns:xfa="soommmeee-tteexxxxtttttt">
    <students>
        <id>B</id>
        <name>jzvxcbhjnba</name>
        <description>mjZSVxcj</description>
        <student>
            <id>3</id>
            <refObjectId>m8</refObjectId>
            <subject>
                <id>91</id>
                <name>humanities</name>
                <friend>hercules</friend>
                <firstname>
                    <textContent>
                        <Id>m954</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Current</body>
                        </text>
                        <Key>Title</Key>
                    </textContent>
                    <textContent>
                        <Id>m9542</Id>
                        <title xfa:contentType="text/html">
                            <body>SubTitle</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>Sr</body>
                        </text>
                        <Key>SubTitle</Key>
                    </textContent>
                    <isDisplay>false</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>aksbgdk</fragId>
                            <znjdvgsc>zsdbZXSBGCDSNXCJK</znjdvgsc>
                            <zmdcb>slzdhfksjfml</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
        <student>
            <id>4</id>
            <refObjectId>m3</refObjectId>
            <subject>
                <id>75</id>
                <name>PCM</name>
                <friend>hercules</friend>
                <firstname>
                    <textContent>
                        <Id>j4423</Id>
                        <title xfa:contentType="text/html">
                            <body>Title</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>dfzxsdbjchA</body>
                        </text>
                        <Key>zdbgcfjkbna</Key>
                    </textContent>
                    <textContent>
                        <Id>m95</Id>
                        <title xfa:contentType="text/html">
                            <body>zdjbhfjkcda</body>
                        </title>
                        <text xfa:contentType="text/html">
                            <body>sdjkhzjk</body>
                        </text>
                        <Key>mzsdjkbjk</Key>
                    </textContent>
                    <isDisplay>true</isDisplay>
                </firstname>
                <lastname>
                    <Detail>
                        <lastNameDetail>
                            <fragId>sdbnfn</fragId>
                            <znjdvgsc>masdjkljk</znjdvgsc>
                            <zmdcb>msjkldbhfjkn</zmdcb>
                        </lastNameDetail>
                    </Detail>
                </lastname>
            </subject>
        </student>
    </students>
    <faculty>
        <configuration>
            <Type>Output</Type>
            <Key>Font</Key>
            <Value>10pt</Value>
        </configuration>
        <configuration>
            <Type>Out</Type>
            <Key>Text</Key>
            <Value>127,127,127</Value>
        </configuration>
        <configuration>
            <Type>put</Type>
            <Key>Slide</Key>
            <Value>18pt</Value>
        </configuration>
        <configuration>
            <Type>utp</Type>
            <Key>Slide</Key>
            <Value>127</Value>
        </configuration>
    </faculty>
    <Info />
    <Dean>
        <name>zcdfjkaqbkd</name>
    </Dean>
</class>        

我想我对这个想法很清楚,如果有什么我可以做的解释更多,请评论......感谢现在只有

以下是我现在正在使用的xsl:

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

    <xsl:output omit-xml-declaration="yes" indent="yes" />
    <xsl:strip-space elements="*" />

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

    <xsl:template match="/">
        <xsl:variable name="vResultSelected">
            <xsl:apply-templates mode="Selected" />
        </xsl:variable>

        <xsl:for-each select="1 to 2">
            <xsl:result-document
                href="SectionOutput\{'Selected'[position()=current()]}.xml">
                <xsl:copy-of select="($vResultSelected)[position() = current()]" />
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>

    <xsl:template mode="Selected"
        match="students/*[not(*/friend = 'hercules')]" />
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

我认为你的方法有几个缺陷:

  • 首先,您的方法是复杂的。您正在使用output-document,尽管您对单个XML outfile感兴趣。如果您必须在几个 XML输出文件中拆分结果,output-document通常会派上用场。如果您有此需要,您应该相应地更新您的预期结果。
  • 您正在为模板使用多个不同的mode,但模式不会在相应的apply-templates中调用,因此,作为效果,它们永远不会被调用。
  • 您使用的选择语法1 to 2无效,因为select应包含xquery表达式。另外,你怎么知道它会是1到2?这只对这个具体的XML输入文件有效,不是吗?

请在下面找到您的问题的建议。

<?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" version="1.0" encoding="UTF-8" indent="yes" />

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

  <xsl:template match="students[not(.//friend = 'hercules')]"/>

  <xsl:template match="student[not(.//friend = 'hercules')]"/>

</xsl:stylesheet>

它基本上使用三个元素:

  • 匹配*|@*的模板,确保我们将所有内容复制到未由更具体的模板明确处理的输出文件。
  • students标记匹配的模板,该标记没有至少一个student与名为“hercules”的朋友。对于这些,我们不做任何事情导致students的整个子树被丢弃。
  • 与没有朋友的student匹配的模板名为“hercules”。对于这些,我们不做任何事情导致student的整个子树被丢弃。