Xml 如何将节点=值对与&;断路器

Xml 如何将节点=值对与&;断路器,xml,xslt,Xml,Xslt,我需要使用xslt将xml结构转换为文本字符串。我的xml结构如下所示: <index> <account index="0">00000000000</account> <customerId index="0">1112xxxxxxx</customerId> <authorization>1</authorization> <access>1</access> &

我需要使用xslt将xml结构转换为文本字符串。我的xml结构如下所示:

<index>
  <account index="0">00000000000</account>
  <customerId index="0">1112xxxxxxx</customerId>
  <authorization>1</authorization>
  <access>1</access>
  <documentGroup>1</documentGroup>
  <documentType>165200</documentType>
  <!-- Any number of child nodes -->
</index>
account=00000000000&customerId=1112xxxxxxx&authorization=1.....

关于如何实现这一点有什么想法吗?

像这样的想法应该可以满足您的需要。您可能需要注意使用
进行实体编码&
虽然如此,但是xsl:output method=“text”应该注意以下事项:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <xsl:output method="text"/>  
   <xsl:template match="index">
       <xsl:variable name="len" select="count(*)"/>
       <xsl:for-each select="*">
            <xsl:value-of select="name()"/>=<xsl:value-of select="."/><xsl:choose>
                <xsl:when test="position() &lt; $len">&amp;</xsl:when>
            </xsl:choose>
       </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

=
&;
不过,这不会“转义”字符串(即将空格之类的内容转换为%20),这可能会导致您出现问题,但会适用于任何数量的子节点,我认为这是您面临的主要问题?


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes"/>

    <xsl:template match="index">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:value-of select="concat(local-name(), '=', .)"/>
        <xsl:if test="following-sibling::*">
            <xsl:text>&amp;</xsl:text>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
&;
请注意,如果您不限于XSLT 1.0,则可以使用扩展的XSLT 2.0
xsl:value of
并将所有内容简化为单个模板:

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

    <xsl:template match="index">
        <xsl:value-of select="*/concat(local-name(),'=',.)" separator="&amp;"/>
    </xsl:template>

</xsl:stylesheet>

即使在XSLT 1.0中,您也可以将所有内容简化为单个模板,而无需采用任何迭代指令:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="index/*">
        <xsl:if test="position()>1">
            <xsl:text>&amp;</xsl:text>
        </xsl:if>
        <xsl:value-of select="concat(local-name(), '=', .)"/>
    </xsl:template>

</xsl:stylesheet>

&;

太棒了!非常感谢,它工作得很好:“我做的唯一改变是使用Loal-NAMEL()而不是NAME()来去掉任何命名空间前缀。但是@和Erik:但是请考虑并考虑接受更好的解决方案。这是三个最好的解决方案,比接受的答案要好得多。”1.谢谢@Dimitre,你让我振奋起来。酷:-),我也是+1。这个答案是在接受actionshrimp的解决方案后得出的,但我当然会将其设置为接受的解决方案:)感谢所有贡献者!