面向XML编写xslt的困难。

时间:2022-10-02 17:19:41

This first time I am dealing with xslt creations, finding it really tricky.

这是我第一次处理xslt创建,发现它非常棘手。

I have request XML and using xslt it is supposed to generate XML with below mentioned o/p.

我已经请求了XML并使用xslt,它应该生成下面提到的o/p的XML。

Utility performing this operation is already built in C#. I need to write XSLT that will correctly read values from XML and create new XML with expected format.

执行此操作的实用程序已经在c#中构建。我需要编写XSLT,它将正确读取XML中的值并创建具有预期格式的新XML。

I am trying to write XSLT to transform XML(i/p) :

我正在尝试编写XSLT来转换XML(I /p):

    <s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" 
    xmlns:s="http://www.w3.org/2003/05/soap-envelope">
    <s:Body>
        <ExecuteESI xmlns="http://TTTT.com/Enterprise/ServiceGateways/">
            <context xmlns:b="http://TTTT.com/Enterprise/ServiceGateways/Core/Contracts/" 
                xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <request xmlns:b="http://TTTT.com/Enterprise/ServiceGateways/ExternalService/Request/" 
                    xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                    <b:Message>
                        <ns2:ReqProv xmlns:ns2="http://TTTT.com/TTTT/TTTTestJSONESIRequest/">
                            <ns2:CID>TTTT</ns2:CID> 
                            <ns2:TIME>18732081720160855</ns2:TIME> 
                            <ns2:HASH>40dad29c4ae9303c975639f7700bf517e6e72fbf5723c7905a74fde0d5452da0</ns2:HASH> 
                        </ns2:ReqProv>
                    </b:Message>
                </request>
            </context>
        </ExecuteESI>
    </s:Body>
</s:Envelope>

to Below mentioned format (o/p):

以下格式(o/p):

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">CID=TTTT&amp;TIME=18732081720160855&amp;HASH=40dad29c4ae9303c975639f7700bf517e6e72fbf5723c7905a74fde0d545</string>

I have written following XSLT, which doesn't work

我已经按照XSLT编写了代码,它不起作用

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:ns="http://asurion.com/TTTT/TTTTTestJSONESIRequest/"
                xmlns="http://schemas.microsoft.com/2003/10/Serialization/"
                exclude-result-prefixes="msxsl xsl ns">
  <xsl:output method="xml" />
  <xsl:template match="/">

    <xsl:variable name="CID">
      <xsl:value-of select="./ns:ReqProv/ns:CID"/>
    </xsl:variable>
    <xsl:variable name="TIME">
      <xsl:value-of select="./ns:ReqProv/ns:TIME"/>
    </xsl:variable>
    <xsl:variable name="HASH">
      <xsl:value-of select="./ns:ReqProv/ns:HASH"/>
    </xsl:variable>

    CID=<xsl:value-of select="$CID" />&amp;TIME=<xsl:value-of select="$TIME" />&amp;HASH=<xsl:value-of select="$HASH" />

  </xsl:template>

</xsl:stylesheet>

I have tried different combinations of <xsl:value-of... and <xsl:template match... and <xsl:template name.... but nothings seems to work.

我尝试过不同组合的

3 个解决方案

#1


1  

Try this:

试试这个:

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:ns="http://asurion.com/TTTT/TTTTTestJSONESIRequest/"
            xmlns="http://schemas.microsoft.com/2003/10/Serialization/"
            xmlns:s="http://www.w3.org/2003/05/soap-envelope"
            xmlns:ns2="http://TTTT.com/TTTT/TTTTestJSONESIRequest/"
            exclude-result-prefixes="msxsl xsl ns">
<xsl:output method="xml" />
<xsl:template match="/">

<xsl:variable name="CID">
  <xsl:value-of select="//ns2:ReqProv/ns2:CID"/>
</xsl:variable>
<xsl:variable name="TIME">
  <xsl:value-of select="//ns2:ReqProv/ns2:TIME"/>
</xsl:variable>
<xsl:variable name="HASH">
  <xsl:value-of select="//ns2:ReqProv/ns2:HASH"/>
</xsl:variable>

<xsl:element name="string">CID=<xsl:value-of select="$CID" />&amp;TIME=<xsl:value-of select="$TIME" />&amp;HASH=<xsl:value-of select="$HASH" /></xsl:element>

</xsl:template>

</xsl:stylesheet>

Needs to change

需要改变

<xsl:value-of select="./ns:ReqProv/ns:CID"/>

to

<xsl:value-of select="//ns2:ReqProv/ns2:CID"/>

Because, in put xml ns2 is the namespace for the element CID.

因为,在put xml ns2中是元素CID的命名空间。

OutPut:

输出:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">CID=TTTT&amp;TIME=18732081720160855&amp;HASH=40dad29c4ae9303c975639f7700bf517e6e72fbf5723c7905a74fde0d5452da0</string>

#2


2  

There are numerous problems with your attempt - the most serious one is using the wrong namespace. Try instead:

您的尝试有很多问题——最严重的问题是使用错误的名称空间。试一试:

XSLT 1.0

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://TTTT.com/TTTT/TTTTestJSONESIRequest/"
xmlns="http://schemas.microsoft.com/2003/10/Serialization/"
exclude-result-prefixes="ns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <string>
        <xsl:text>CID=</xsl:text>
        <xsl:value-of select="//ns:ReqProv/ns:CID"/>

        <xsl:text>@amp;TIME=</xsl:text>
        <xsl:value-of select="//ns:ReqProv/ns:TIME"/>

        <xsl:text>@amp;HASH=</xsl:text>
        <xsl:value-of select="//ns:ReqProv/ns:HASH"/>
    </string>
</xsl:template>

</xsl:stylesheet>

Note:

注意:

  • there is very little point in defining a variable for something you only need once;

    为你只需要一次的东西定义一个变量是毫无意义的;

  • it is much more convenient to use the xsl:text instruction when outputting literal text; it also makes the code easier to read and maintain.

    输出文本时使用xsl:text指令要方便得多;它还使代码更易于阅读和维护。

#3


1  

You're almost there. You need to supply the <string> result element in the stylesheet template, fix your XPath expressions, and fix a mismatched namespace URI.

你差不多了。您需要在样式表模板中提供 结果元素,修复XPath表达式,并修复不匹配的名称空间URI。

You'll want to change your template that matches "/" to something like the following:

您将希望将与“/”匹配的模板更改为以下内容:

  <xsl:template match="/">
    <!-- No need to use value-of here.
     Also use .// instead of ./, because ns:ReqProv is not a child of /. -->
    <xsl:variable name="CID" select=".//ns:ReqProv/ns:CID"/>
    <xsl:variable name="TIME" select=".//ns:ReqProv/ns:TIME"/>
    <xsl:variable name="HASH" select=".//ns:ReqProv/ns:HASH"/>

    <string>CID=<xsl:value-of select="$CID" />&amp;TIME=<xsl:value-of select="$TIME" />&amp;HASH=<xsl:value-of select="$HASH" /></string>

  </xsl:template>

You also need to change one of these two namespace URIs so that they match, in the input XML and the XSLT:

您还需要更改这两个名称空间uri中的一个,以便它们在输入XML和XSLT中匹配:

"http://TTTT.com/TTTT/TTTTestJSONESIRequest/"

and

"http://asurion.com/TTTT/TTTTTestJSONESIRequest/"

I'm guessing these are really the same in your actual input XML and XSLT, but you changed one of them in posting the question here. So you'll want to make sure that they match.

我猜这些在实际的输入XML和XSLT中都是一样的,但是您在这里的问题中修改了其中的一个。所以你要确保它们匹配。

#1


1  

Try this:

试试这个:

<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:ns="http://asurion.com/TTTT/TTTTTestJSONESIRequest/"
            xmlns="http://schemas.microsoft.com/2003/10/Serialization/"
            xmlns:s="http://www.w3.org/2003/05/soap-envelope"
            xmlns:ns2="http://TTTT.com/TTTT/TTTTestJSONESIRequest/"
            exclude-result-prefixes="msxsl xsl ns">
<xsl:output method="xml" />
<xsl:template match="/">

<xsl:variable name="CID">
  <xsl:value-of select="//ns2:ReqProv/ns2:CID"/>
</xsl:variable>
<xsl:variable name="TIME">
  <xsl:value-of select="//ns2:ReqProv/ns2:TIME"/>
</xsl:variable>
<xsl:variable name="HASH">
  <xsl:value-of select="//ns2:ReqProv/ns2:HASH"/>
</xsl:variable>

<xsl:element name="string">CID=<xsl:value-of select="$CID" />&amp;TIME=<xsl:value-of select="$TIME" />&amp;HASH=<xsl:value-of select="$HASH" /></xsl:element>

</xsl:template>

</xsl:stylesheet>

Needs to change

需要改变

<xsl:value-of select="./ns:ReqProv/ns:CID"/>

to

<xsl:value-of select="//ns2:ReqProv/ns2:CID"/>

Because, in put xml ns2 is the namespace for the element CID.

因为,在put xml ns2中是元素CID的命名空间。

OutPut:

输出:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">CID=TTTT&amp;TIME=18732081720160855&amp;HASH=40dad29c4ae9303c975639f7700bf517e6e72fbf5723c7905a74fde0d5452da0</string>

#2


2  

There are numerous problems with your attempt - the most serious one is using the wrong namespace. Try instead:

您的尝试有很多问题——最严重的问题是使用错误的名称空间。试一试:

XSLT 1.0

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://TTTT.com/TTTT/TTTTestJSONESIRequest/"
xmlns="http://schemas.microsoft.com/2003/10/Serialization/"
exclude-result-prefixes="ns">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <string>
        <xsl:text>CID=</xsl:text>
        <xsl:value-of select="//ns:ReqProv/ns:CID"/>

        <xsl:text>@amp;TIME=</xsl:text>
        <xsl:value-of select="//ns:ReqProv/ns:TIME"/>

        <xsl:text>@amp;HASH=</xsl:text>
        <xsl:value-of select="//ns:ReqProv/ns:HASH"/>
    </string>
</xsl:template>

</xsl:stylesheet>

Note:

注意:

  • there is very little point in defining a variable for something you only need once;

    为你只需要一次的东西定义一个变量是毫无意义的;

  • it is much more convenient to use the xsl:text instruction when outputting literal text; it also makes the code easier to read and maintain.

    输出文本时使用xsl:text指令要方便得多;它还使代码更易于阅读和维护。

#3


1  

You're almost there. You need to supply the <string> result element in the stylesheet template, fix your XPath expressions, and fix a mismatched namespace URI.

你差不多了。您需要在样式表模板中提供 结果元素,修复XPath表达式,并修复不匹配的名称空间URI。

You'll want to change your template that matches "/" to something like the following:

您将希望将与“/”匹配的模板更改为以下内容:

  <xsl:template match="/">
    <!-- No need to use value-of here.
     Also use .// instead of ./, because ns:ReqProv is not a child of /. -->
    <xsl:variable name="CID" select=".//ns:ReqProv/ns:CID"/>
    <xsl:variable name="TIME" select=".//ns:ReqProv/ns:TIME"/>
    <xsl:variable name="HASH" select=".//ns:ReqProv/ns:HASH"/>

    <string>CID=<xsl:value-of select="$CID" />&amp;TIME=<xsl:value-of select="$TIME" />&amp;HASH=<xsl:value-of select="$HASH" /></string>

  </xsl:template>

You also need to change one of these two namespace URIs so that they match, in the input XML and the XSLT:

您还需要更改这两个名称空间uri中的一个,以便它们在输入XML和XSLT中匹配:

"http://TTTT.com/TTTT/TTTTestJSONESIRequest/"

and

"http://asurion.com/TTTT/TTTTTestJSONESIRequest/"

I'm guessing these are really the same in your actual input XML and XSLT, but you changed one of them in posting the question here. So you'll want to make sure that they match.

我猜这些在实际的输入XML和XSLT中都是一样的,但是您在这里的问题中修改了其中的一个。所以你要确保它们匹配。