XSL replace space with caret

你。 提交于 2019-12-14 03:55:07

问题


UPDATED CODE:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="no"/>
  <!-- overwritten by application with actual values -->
  <xsl:param name="calling" select="'SAMPLE_MOD'"/>
  <xsl:param name="called" select="'SERVER1'"/>
  <xsl:param name="date" select="'20051206'"/>
  <xsl:param name="time" select="'115600.000'"/>
  <xsl:param name="PatName" select="attr[@tag='00100010']"/>
  <xsl:template match="/dataset">
     <dataset>
     <xsl:variable name="PerfProcStepDesc" select="attr[@tag='00400254']"/>
     <xsl:variable name="StudyDesc" select="attr[@tag='00081030']"/>
       <xsl:if test="string-length($StudyDesc)=0">
         <xsl:if test="$PerfProcStepDesc">
            <!-- (0008,1030) Study Description -->
           <attr tag="00081030" vr="LO">
             <xsl:value-of select="$PerfProcStepDesc"/>
           </attr>
         </xsl:if>
       </xsl:if>
     </dataset>
     <dataset>
        <xsl:variable name="caret" select="'^'"/>
        <xsl:if test="contains($PatName, ' ')">
            <xsl:value-of select="translate($PatName, ' ','^')"/>
        </xsl:if>   
     </dataset>
  </xsl:template>
</xsl:stylesheet>

The variable 'PatName' could come across as 'DOE JOHN' and I need it be transformed to 'DOE^JOHN' There could also be multiple spaces in the name, so I would like all 'spaces' changed to carets and to keep their current place in the name.

I need to do the same with commas that are in the name.

Currently the output coming out just as it went in, so my test is not working as it should.

thanks for looking!

EDIT #2:

I have changed my code to apply a identity transform (hopefully I have it correct!) Also I have the XSL input and XSL output for when it processed my XSL stylesheet.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="no"/>
  <!-- overwritten by application with actual values -->
  <xsl:param name="calling" select="'SAMPLE_MOD'"/>
  <xsl:param name="called" select="'SERVER1'"/>
  <xsl:param name="date" select="'20051206'"/>
  <xsl:param name="time" select="'115600.000'"/>
  <xsl:param name="PatName" select="attr[@tag='00100010']"/>
<!-- IdentityTransform -->
<xsl:template match="/ | @* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>
  <xsl:template match="/dataset">
     <dataset>
     <xsl:variable name="PerfProcStepDesc" select="attr[@tag='00400254']"/>
     <xsl:variable name="StudyDesc" select="attr[@tag='00081030']"/>
       <xsl:if test="string-length($StudyDesc)=0">
         <xsl:if test="$PerfProcStepDesc">
            <!-- (0008,1030) Study Description -->
           <attr tag="00081030" vr="LO">
             <xsl:value-of select="$PerfProcStepDesc"/>
           </attr>
         </xsl:if>
       </xsl:if>
     </dataset>
     <dataset>
        <xsl:variable name="caret" select="'^'"/>
        <xsl:if test="contains($PatName, ' ')">
            <xsl:value-of select="translate($PatName, ' ','^')"/>
        </xsl:if>   
     </dataset>
  </xsl:template>
</xsl:stylesheet>

XSL Input here

XSL Output is below:

<?xml version="1.0" encoding="UTF-8"?>
<dataset/>

in the XSL input, 'Patient Name' there is 'SMPTE PATTERN' and I am expecting 'SMPTE^PATTERN'.

I hope this of some help and I hope I have the identity transformer correct.

thanks for everybodys time


回答1:


You can use translate function.

<xsl:value-of select="translate($PatName, ' ','^')"/>

If you want to replace both comma and space with carets, use this:

<xsl:value-of select="translate($PatName, ' ,','^^')"/>



回答2:


This is not an answer, but I need to post some code.

Please apply the following stylesheet to your (original) input and report the results:

<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:variable name="patName" select="/dataset/attr[@tag='00100010']" />

<xsl:template match="/">
    <tests>
        <value><xsl:value-of select="$patName" /></value>
        <contains><xsl:value-of select="contains($patName, '&#32;')" /></contains>
        <trans><xsl:value-of select="translate($patName, '&#32;' , '^')" /></trans>
    </tests>
</xsl:template>

</xsl:stylesheet> 



回答3:


change

<xsl:param name="PatName" select="attr[@tag='00100010']"/>

into

<xsl:param name="PatName" select="dataset/attr[@tag='00100010']"/>

and change

<xsl:value-of select="translate($PatName, ' ','^')"/>

into

<xsl:value-of select="translate($PatName, ' ,','^^')"/>


来源:https://stackoverflow.com/questions/22361114/xsl-replace-space-with-caret

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!